home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-04-30 | 6.1 KB | 231 lines | [TEXT/MPS ] |
- // ===========================================================================
- // CTextDoc.cp ©1994 Metrowerks Inc. All rights reserved.
- // ===========================================================================
- //
- // A simple text document class. It can handle opening, saving, and
- // reverting TEXT files. It displays the text in a Window and can
- // print the text.
-
- #include "CTextDoc.h"
- #include "CDirtyText.h"
-
- #include <LWindow.h>
- #include <LFile.h>
- #include <LPrintout.h>
- #include <LPlaceHolder.h>
- #include <UMemoryMgr.h>
- #include <UWindows.h>
- #include <String_Utils.h>
-
- const ResIDT WIND_TextDoc = 200;
- const ResIDT prto_TextDoc = 201;
- const OSType Creator_DemoDoc = 'PPd0';
- const ResIDT STRx_Untitled = 300;
-
-
- // ---------------------------------------------------------------------------
- // • CTextDoc(LCommander*, FSSpec*)
- // ---------------------------------------------------------------------------
- // Construct a TextDoc associated with the specified file
- //
- // If inFileSpec is nil, then create an empty, untitled document
-
-
- CTextDoc::CTextDoc(
- LCommander *inSuper,
- FSSpec *inFileSpec)
- : LSingleDoc(inSuper)
- {
- // Create window for our document
- mWindow = LWindow::CreateWindow(WIND_TextDoc, this);
-
- // Specify that the text view should
- // be the Target when the Window
- // is activated
- mTextView = (CDirtyText*) mWindow->FindPaneByID('Text');
- mWindow->SetLatentSub(mTextView);
-
- if (inFileSpec == nil) {
- NameNewDoc(); // Set name of untitled window
-
- } else {
- OpenFile(*inFileSpec); // Display contents of file in window
- }
- }
-
-
- // ---------------------------------------------------------------------------
- // • NameNewDoc
- // ---------------------------------------------------------------------------
- // Name a new, untitled document window
- //
- // Untitled windows start with "untitled", then "untitled 1",
- // "untitled 2", etc. Old numbers are reused, so there won't be
- // gaps in the numbering.
- //
- // This routine uses a STR# resource to store the "untitled" string,
- // which can be localized to different languages. The first string
- // is "untitled" and the second is "untitled " (trailing space),
- // which is used when appending a number to the name.
-
- void
- CTextDoc::NameNewDoc()
- {
- // Start with the default name ("untitled")
- Str255 name;
- ::GetIndString(name, STRx_Untitled, 1);
-
- long num = 0;
- while (UWindows::FindNamedWindow(name) != nil) {
-
- // An existing window has the current name
- // Increment counter and try again
-
- ::GetIndString(name, STRx_Untitled, 2);
- num++;
- Str15 numStr;
- ::NumToString(num, numStr);
- ConcatPStr(name, numStr);
- }
-
- mWindow->SetDescriptor(name); // Finally, set window title
- }
-
-
- // ---------------------------------------------------------------------------
- // • OpenFile
- // ---------------------------------------------------------------------------
- // Open a new document for the specified File
-
- void
- CTextDoc::OpenFile(
- FSSpec &inFileSpec)
- {
- // Create a new File object, read the entire File contents,
- // put the contents into the text view, and set the Window
- // title to the name of the File.
-
- Try_ {
- mFile = new LFile(inFileSpec);
- mFile->OpenDataFork(fsRdWrPerm);
- Handle textH = mFile->ReadDataFork();
- mTextView->SetTextHandle(textH);
- ::DisposeHandle(textH);
-
- mWindow->SetDescriptor(inFileSpec.name);
- mIsSpecified = true;
- }
-
- Catch_(inErr) {
- delete this;
- Throw_(inErr);
-
- } EndCatch_
- }
-
-
- // ---------------------------------------------------------------------------
- // • IsModified
- // ---------------------------------------------------------------------------
- // Return whether the Document is has changed since the last save
-
- Boolean
- CTextDoc::IsModified()
- {
- // Document has changed if the text view is dirty
- mIsModified = mTextView->IsDirty();
- return mIsModified;
- }
-
-
- // ---------------------------------------------------------------------------
- // • DoAESave
- // ---------------------------------------------------------------------------
- // Save Document in the specified file with the specified file type
- //
- // If file type is fileType_Default, use the normal file type for
- // this document
-
- void
- CTextDoc::DoAESave(
- FSSpec &inFileSpec,
- OSType inFileType)
- {
- delete mFile; // Kill existing file
-
- mFile = new LFile(inFileSpec); // Make new file object
-
- OSType fileType = 'TEXT'; // Find proper file type
- if (inFileType != fileType_Default) {
- fileType = inFileType;
- }
- // Make new file on disk
- mFile->CreateNewDataFile(Creator_DemoDoc, 'TEXT', 0);
- mFile->OpenDataFork(fsRdWrPerm);
- DoSave(); // Write out data
- // Change window name
- mWindow->SetDescriptor(inFileSpec.name);
- }
-
-
- // ---------------------------------------------------------------------------
- // • DoSave
- // ---------------------------------------------------------------------------
- // Save the entire Document to its associated File (which must already exist)
-
- void
- CTextDoc::DoSave()
- {
- // Get text and write to file
- Handle textH = mTextView->GetTextHandle();
- StHandleLocker theLock(textH);
- mFile->WriteDataFork(*textH, GetHandleSize(textH));
-
- mTextView->SetDirty(false); // Saving makes doc un-dirty
- }
-
-
- // ---------------------------------------------------------------------------
- // • DoRevert
- // ---------------------------------------------------------------------------
- // Revert the Document to the last saved version on disk
-
- void
- CTextDoc::DoRevert()
- {
- Handle textH = mFile->ReadDataFork();
- mTextView->SetTextHandle(textH);
- ::DisposeHandle(textH);
- mTextView->Refresh();
- }
-
-
- // ---------------------------------------------------------------------------
- // • DoPrint
- // ---------------------------------------------------------------------------
- // Print the contents of the Document
-
- void
- CTextDoc::DoPrint()
- {
- LPrintout *thePrintout = LPrintout::CreatePrintout(prto_TextDoc);
- LPlaceHolder *textPlace = (LPlaceHolder*)
- thePrintout->FindPaneByID('TBox');
- textPlace->InstallOccupant(mTextView, atNone);
-
- thePrintout->DoPrintJob();
- delete thePrintout;
- }
-
- void
- CTextDoc::OutString(char* str, int len)
- {
- TEInsert(str, len, mTextView->GetMacTEH());
- }
-
-
- void
- CTextDoc::SetDescriptor(ConstStr255Param name)
- {
- mWindow->SetDescriptor(name);
- }